package com.zx.service.impl;

import com.alibaba.fastjson.JSON;
import com.zx.entity.Order;
import com.zx.entity.Product;
import com.zx.feign.ProductFeignClient;
import com.zx.mapper.OrderMapper;
import com.zx.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020-03-08
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private DiscoveryClient discoveryClient;

    @Resource
    private ProductFeignClient productFeignClient;

    @Resource
    private RocketMQTemplate rocketMQTemplate;


//    @Override
//    public Order addOrder(Integer pid) {
//        log.info("开始调用商品服务，查询商品id:{}的商品",pid);
//        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
//        // 自定义负载均衡
//        int index = new Random().nextInt(instances.size());
//        ServiceInstance instance = instances.get(index);
//        Product product = restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/product/" + pid, Product.class);
//        log.info("查询到{}号商品信息，内容{}",pid, JSON.toJSONString(product));
//        Order order = new Order();
//        order.setUid(1);
//        order.setUsername("system");
//        order.setNumber(1);
//        order.setPid(product.getPid());
//        order.setPname(product.getPname());
//        order.setPprice(product.getPprice());
//        Order o = orderMapper.save(order);
//        log.info("创建订单成功，订单信息为：{}",JSON.toJSONString(order));
//        return o;
//    }

    @Override
    @GlobalTransactional
    public Order addOrder(Integer pid) {
        log.info("开始调用商品服务，查询商品id:{}的商品",pid);
        /**
         * 使用restTemplate中存在的问题
         * 1、一旦服务提供者的地址发放变化需要修改Java代码
         * 2、一旦服务提供者做了集群，服务调用者无法实现负载均衡去调用
         * 3、一旦微服务变得越来越多，不好管理服务清单
         */
       // Product product = restTemplate.getForObject("http://service-product/product/product/" + pid, Product.class);
        Product product = productFeignClient.findById(pid);
        if (product.getPid() == -1){
            Order order = new Order();
            order.setOid(-1L);
            order.setPname("下单失败");
            return order;
        }
        log.info("查询到{}号商品信息，内容{}",pid, JSON.toJSONString(product));
        Order order = new Order();
        order.setUid(1);
        order.setUsername("system");
        order.setNumber(1);
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        Order o = orderMapper.save(order);
        // 远程调用商品微服务-扣库存
        productFeignClient.reduceInventory(pid,order.getNumber());
        // 向order-topic投递消息
//        rocketMQTemplate.convertAndSend("order-topic",order);
        log.info("创建订单成功，订单信息为：{}",JSON.toJSONString(order));
        return o;
    }

}
